import base64
import os
import uuid

import requests

from common import constants
from common.util import Util

# url = "http://192.168.200.87:4000/rpc"
url = "http://192.168.8.244:4000/rpc"

app_key = "APP_KEY",
app_secret = "APP_SECRET",
open_conversation_id = "OPEN_CONVERSATION_ID",
robot_code = "ROBOT_CODE"


def upload_file_to_ding():
    screenshot_name = "000.jpg"
    screenshot_dir = Util.unified_static_path(constants.FILES_SAVE_DIRECTORY)
    screenshot_path = os.path.join(screenshot_dir, screenshot_name)
    with open(screenshot_path, 'rb') as file:
        file_content = base64.b64encode(file.read()).decode('utf-8')
    payload = {
        "jsonrpc": "2.0",
        "method": "upload_file_to_ding",
        "params": {
            "file_name": screenshot_name,
            "file_content": file_content,
            "context": {
                "app_key": app_key,
                "app_secret": app_secret,
                "open_conversation_id": open_conversation_id,
                "robot_code": robot_code
            }
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def upload_images():
    screenshot_name = "000.png"
    screenshot_dir = Util.unified_static_path(constants.FILES_SAVE_DIRECTORY)
    screenshot_path = os.path.join(screenshot_dir, screenshot_name)
    with open(screenshot_path, 'rb') as file:
        file_content = base64.b64encode(file.read()).decode('utf-8')
    payload = {
        "jsonrpc": "2.0",
        "method": "upload_images",
        "params": {
            "bucket_name": "crawler",
            "object_name": "/screenshot/000.png",
            "file_content": file_content
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def upload_file():
    screenshot_name = "000.png"
    screenshot_dir = Util.unified_static_path(constants.FILES_SAVE_DIRECTORY)
    screenshot_path = os.path.join(screenshot_dir, screenshot_name)
    with open(screenshot_path, 'rb') as file:
        file_content = base64.b64encode(file.read()).decode('utf-8')
    payload = {
        "jsonrpc": "2.0",
        "method": "upload_file",
        "params": {
            "bucket_name": "crawler",
            "object_name": "/screenshot/000.png",
            "file_content": file_content
        },
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    print(response)


def download():
    payload = {
        "jsonrpc": "2.0",
        "method": "download",
        "params": {"bucket_name": "crawler",
                   "object_name": "/screenshot/689264c8-0061-4769-87cc-a12b9a45c4fb.png"},
        "id": str(uuid.uuid4())
    }
    # 发送请求
    response = requests.post(url, json=payload).json()
    data = response["data"]
    decoded_content = base64.b64decode(data)
    screenshot_dir = Util.unified_static_path(constants.FILES_SAVE_DIRECTORY)
    if not os.path.exists(screenshot_dir):
        os.makedirs(screenshot_dir)
    screenshot_name = "000.png"
    screenshot_path = os.path.join(screenshot_dir, screenshot_name)
    with open(screenshot_path, 'wb') as file:
        file.write(decoded_content)


if __name__ == '__main__':
    upload_file_to_ding()
